﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DatabaseExtension
{
    public static class EnumExtension
    {
        /// <summary>
        /// Defines the Enumerator String value separation Character for multiple words. Needs to convert String To Enum.
        /// </summary>
        private const Char EnumSeparator = '_';
        /// <summary>
        /// Defines the String separator Character for multiple words. Needs to convert String To Enum.
        /// </summary>
        private const Char StringSeparator = ' ';
        /// <summary>
        /// Parse Enum to String with Spaces before Upper Case Character.
        /// </summary>
        /// <param name="e">Enumerator to parse.</param>
        /// <returns>Well Formatted String</returns>
        public static String EnumToString(this Enum e)
        {
            String data = e.ToString(), result = null;
            if (String.Equals("None", data) == false)
                result = data.Replace(EnumExtension.EnumSeparator, EnumExtension.StringSeparator);
            return result;
        }
        /// <summary>
        /// Parse Enumerator to String with space.
        /// </summary>
        /// <param name="enumType">Enumerator Type.</param>
        /// <param name="data">String to Translate</param>
        public static Object StringToEnum(this String data, Type enumType)
        {
            if (String.IsNullOrEmpty(data))
                data = "None";
            return Enum.Parse(enumType, data.Replace(EnumExtension.StringSeparator, EnumExtension.EnumSeparator), true);
        }
    }
}
